const http = require('../../../utils/http.js')
const Base64 = require('../../../utils/base64.js')

Page({

    /**
     * 页面的初始数据
     */
    data: {
        imgUrl: http.imgUrl,
        inputArr: [],
        canClick: true
    },

    /**
     * 返回
     */
    goBack() {
        wx.navigateBack({
            delta: 1
        })
    },

    /**
     *  获取所有INPUT的数据
     */
    inputTxt(e) {
        let inputArr = this.data.inputArr
        let temp = http.inputArr(inputArr, e)
        // console.log(temp)
        this.setData({
            inputArr: temp
        })
    },

    /**
     * 获取短信验证码
     */
    getVercoe() {
        console.log('验证啊')
        let that = this
        let url = 'User_getMessage'
        let tel = that.data.inputArr[0]
        console.log(tel)
        console.log(that.data)
        if (!http.ifEmpty(tel)) {
            wx.showToast({
                title: '请填写手机号',
                icon: 'none',
                duration: 3000
            })
            return
        }

        if (!http.rexFn(tel)[0]) {
            wx.showToast({
                title: '请输入正确的手机号',
                icon: 'none',
                duration: 3000
            })
            return
        }

        //console.log(Base64.CusBASE64.encoder('UNO' + tel + 'HACHA'))
        http._post(
            url,
          { mobile: Base64.CusBASE64.encoder('UNO' + tel + 'HACHA'), type: 2},
            res => {
                // console.log(res.data)
                let data = res.data
                if (data.re === '1') {
                    wx.showToast({
                        title: data.info,
                        icon: 'none',
                        duration: 3000
                    })
                    that.setData({
                        ifVerify: true,
                        timeDown: 60
                    })
                    let time = 60
                    let timeCount = setInterval(function () {
                        time--
                        that.setData({
                            timeDown: time
                        })
                        if (time <= 0) {
                            that.setData({
                                ifVerify: false
                            })
                            clearInterval(timeCount)
                        }
                    }, 1000)
                } else {
                    wx.showToast({
                        title: data.info,
                        icon: 'none',
                        duration: 3000
                    })
                }
            },
            err => {
                console.log(err)
            }
        )
    },

    /**
     * 忘记密码
     */
    forgotPassword() {
        let that = this,
        url = 'User_forgotPassword',
        inputArr = that.data.inputArr,
        mobile = Base64.CusBASE64.encoder('UNO' + inputArr[0] + 'HACHA'),
        code = inputArr[1],
        // code = Base64.CusBASE64.encoder('UNO' + inputArr[1] + 'HACHA'),
        password = Base64.CusBASE64.encoder('UNO' + inputArr[2] + 'HACHA'),
        repassword = Base64.CusBASE64.encoder('UNO' + inputArr[3] + 'HACHA')
        var passwords = inputArr[2]
        var repasswords = inputArr[3]
      console.log(inputArr)
        if (!inputArr[0]){
          wx.showToast({
            title: '请输入手机号',
            icon: 'none',
            duration: 3000
          })
          return false  
        }
        // if (!http.ifEmpty(mobile)) {
        //     wx.showToast({
        //         title: '请填写手机号',
        //         icon: 'none',
        //         duration: 3000
        //     })
        //     return false
        // }

        if (!http.ifEmpty(code)) {
            wx.showToast({
                title: '请填写验证码',
                icon: 'none',
                duration: 3000
            })
            return false
        }

      if (!passwords) {
            wx.showToast({
                title: '请输入新密码',
                icon: 'none',
                duration: 3000
            })
            return false
        }

        if (!repasswords) {
            wx.showToast({
                title: '请再次输入密码',
                icon: 'none',
                duration: 3000
            })
            return false
        }

        http._post(
            url,
            { mobile: mobile, code: code, password: password, repassword: repassword,type:1 },
            res => {
                console.log(res.data)
                let data = res.data
                if (data.re === '1') {
                    wx.setStorageSync('token', data.data.token)
                    wx.showToast({
                        title: data.info,
                        icon: 'none',
                        duration: 3000,
                        success: function () {
                            setTimeout(() => {
                                wx.reLaunch({
                                    url: '/pages/chooseIdentity',
                                })
                            }, 500)
                        }
                    })
                    that.setData({
                        canClick: false
                    })
                } else {
                    wx.showToast({
                        title: data.info,
                        icon: 'none',
                        duration: 3000
                    })
                }
            },
            err => {
                console.log(err)
            }
        )
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})